package DemoBall;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class BallFrame extends JFrame{
	private BallPanel panel;
	int TocDo = 5;
	public static final int DEFAULT_WIDTH = 450;
	public static final int DEFAULT_HEIGHT = 350;
	JButton btnTangToc = new JButton("Tăng tốc");
	JButton btnGiamToc = new JButton("Giảm tốc");
	int count=1;
	public BallFrame(){
		setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);
		setTitle("CreatBall");
		panel = new BallPanel();
		add(panel , BorderLayout.CENTER);
		JPanel buttonPanel = new JPanel();
		buttonPanel.setBackground(Color.gray); 
		addButton(buttonPanel, "Add_Ball",
			new ActionListener() {
	
				public void actionPerformed(ActionEvent e) {
					addBall();
					}
				}
			);
		btnTangToc.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				
				TocDo -= 5;
				if(TocDo < 0) TocDo = 5;				
			}
		});
		btnGiamToc.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				TocDo += 5;
				//panel.
			}
		});
		buttonPanel.add(btnTangToc);
		buttonPanel.add(btnGiamToc);
		add(buttonPanel, BorderLayout.SOUTH);
	}
	public void addButton(Container c, String title, ActionListener listener){
		JButton bt = new JButton(title);
		c.add(bt);
		bt.addActionListener(listener);
	}
	
	
	Runnable r;
	public void addBall(){
		Ball b = new Ball();
		panel.add(b);
		r = new BallRunnable(b, panel, TocDo);
		Thread t = new Thread(r);
		t.start();
	}
}
